#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;
const int M = 2e5 + 5;

int n, m;
int head[N];
int ver[M << 1], Next[M << 1], tot;
void add(int u, int v) {
  ++tot;
  ver[tot] = v;
  Next[tot] = head[u];
  head[u] = tot;
}

int st[N], top;
int dfn[N], low[N], num;
vector<int> g[N];
void tarjan(int u, int fu) {
  dfn[u] = low[u] = ++num;
  st[++top] = u;
  for (int i = head[u]; i; i = Next[i]) {
    int v = ver[i];
    if (!dfn[v]) {
      tarjan(v, u);
      low[u] = min(low[u], low[v]);
      if (low[v] >= dfn[u]) {
        g[u].push_back(++n);
        do {
          g[n].push_back(st[top]);
        } while (st[top--] != v);
      }
    } else
      low[u] = min(low[u], dfn[v]);
  }
}

int dep[N], fa[N][18];
void dfs(int u, int fu) {
  dfn[u] = ++num;
  dep[u] = dep[fu] + 1;
  fa[u][0] = fu;
  rep(i, 1, 17) fa[u][i] = fa[fa[u][i - 1]][i - 1];
  for (int v : g[u]) {
    dfs(v, u);
  }
}
int lca(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  int d = dep[u] - dep[v];
  per(i, 17, 0) if (d & (1 << i)) u = fa[u][i];
  if (u == v) return u;
  per(i, 17, 0) if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
  return fa[u][0];
}

bool cmp(int x, int y) { return dfn[x] < dfn[y]; }
int query(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  return (dep[u] + 1) / 2 - (dep[v] + 1) / 2;
}
int k, a[N], root;
int build() {
  root = lca(a[1], a[k]);
  int ans = dep[root] & 1;
  st[top = 1] = root;
  if (a[1] != root) st[++top] = a[1];
  rep(i, 2, k) {
    int x = lca(st[top], a[i]);
    while (top > 1 && dep[st[top - 1]] >= dep[x])
      ans += query(st[top - 1], st[top]), top--;
    if (x != st[top]) ans += query(x, st[top]), st[top] = x;
    st[++top] = a[i];
  }
  while (top > 1) ans += query(st[top - 1], st[top]), top--;
  return ans;
}
void solve() {
  cin >> n >> m;
  // 建构图
  rep(i, 1, n) head[i] = 0;
  tot = 0;
  rep(i, 1, m) {
    int u, v;
    cin >> u >> v;
    add(u, v), add(v, u);
  }

  // 构建圆方树
  top = num = 0;
  rep(i, 1, n) dfn[i] = 0;
  rep(i, 1, n * 2) g[i].clear();
  tarjan(1, 0);

  // 在圆方树上准备lca,, dfn序号
  num = 0;
  dfs(1, 0);

  int q;
  cin >> q;
  while (q--) {
    cin >> k;
    rep(i, 1, k) cin >> a[i];
    sort(a + 1, a + k + 1, cmp);
    cout << build() - k << endl;
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}